using Microsoft.AspNetCore.Mvc;
using RAG.Infrastructure.Services;

namespace RAG.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SeedController : ControllerBase
{
    private readonly DataSeederService _seederService;
    private readonly ILogger<SeedController> _logger;

    public SeedController(DataSeederService seederService, ILogger<SeedController> logger)
    {
        _seederService = seederService;
        _logger = logger;
    }

    /// <summary>
    /// 添加测试知识库数据
    /// </summary>
    [HttpPost("knowledge")]
    public async Task<ActionResult> SeedKnowledgeBase()
    {
        try
        {
            var hasData = await _seederService.HasTestDataAsync();
            if (hasData)
            {
                return BadRequest(new { message = "测试数据已存在，请先清理后再重新添加" });
            }

            await _seederService.SeedKnowledgeBaseAsync();
            return Ok(new { message = "成功添加10条测试知识库数据", count = 10 });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "添加测试数据失败");
            return StatusCode(500, new { message = "添加测试数据失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 检查是否有测试数据
    /// </summary>
    [HttpGet("status")]
    public async Task<ActionResult> GetSeedStatus()
    {
        try
        {
            var hasData = await _seederService.HasTestDataAsync();
            return Ok(new { hasTestData = hasData });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "检查测试数据状态失败");
            return StatusCode(500, new { message = "检查状态失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 清理测试数据
    /// </summary>
    [HttpDelete("cleanup")]
    public async Task<ActionResult> CleanupTestData()
    {
        try
        {
            await _seederService.CleanupTestDataAsync();
            return Ok(new { message = "测试数据清理完成" });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "清理测试数据失败");
            return StatusCode(500, new { message = "清理测试数据失败", error = ex.Message });
        }
    }
}
